package org.iteam.xlz.core.service.membershipcard;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.iteam.commons.utils.DateUtil;
import org.iteam.core.utils.SqlBuilder;
import org.iteam.db.BaseService;
import org.iteam.xlz.core.entity.membershipcard.MembershipCard;

public class CardService extends BaseService<MembershipCard>{
	static CardService instance;
	private CardService(){}
	
	public static CardService getInstance(){
		if(null==instance){
			instance=new CardService();
		}
		return instance;
	}
	
	public long counts(String mobile){
		try {
			StringBuilder sql=new StringBuilder("select count(1) from membershipcard l left join userinfo r on l.uid=r.uid where 1=1");
			List<String> params=new ArrayList<String>();
			if(StringUtils.isNotBlank(mobile)){
				sql.append(" and r. mobile=?");
				params.add(mobile);
			}
			return super.counts(sql.toString(),params.toArray());
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	public List<MembershipCard> query(String mobile,long page,long rows){
		try {
			List<Object> params=new ArrayList<Object>();
			StringBuilder sql=new StringBuilder();
			sql.append("select l.mid,u.mobile as uid,r.typename as mtid,l.balane,l.idtype,l.idcard,l.sex,l.address,l.birthday,l.profession,");
			sql.append("l.status,l.createtime,l.creator from membershipcard l left join membershipcardtype r on l.mtid=r.mtid left join userinfo u");
			sql.append(" on l.uid=u.uid where 1=1");
			if(StringUtils.isNotBlank(mobile)){
				sql.append(" and u.mobile=?");
				params.add(mobile);
			}	
			sql.append(" limit ?,?");
			params.add((page-1)*rows);
			params.add(rows);
			return super.query(sql.toString(),params.toArray());
			} catch (SQLException e) {
				e.printStackTrace();
				return new ArrayList<MembershipCard>();
			}
	}
	
	public boolean create(MembershipCard model){
		SqlBuilder sql=SqlBuilder.createInsert("membershipcard");
		sql.addField("mid",UUID.randomUUID().toString().replace("-","").toUpperCase());
		sql.addField("uid",model.getUid());
		sql.addField("mtid",model.getMtid());
		sql.addField("balane",0);
		sql.addField("idtype",model.getIdtype());
		if(StringUtils.isNotBlank(model.getIdcard())){
			sql.addField("idcard",model.getIdcard());
		}
		if(StringUtils.isNotBlank(model.getAddress())){
			sql.addField("address",model.getAddress());
		}
		sql.addField("sex",model.getSex());
		if(StringUtils.isNotBlank(model.getBirthday())){
			sql.addField("birthday",model.getBirthday());
		}
		if(StringUtils.isNotBlank(model.getProfession())){
			sql.addField("profession",model.getProfession());
		}
		sql.addField("status",model.getStatus());
		sql.addField("creator",model.getCreator());
		sql.addField("createtime",DateUtil.currentTime());
		try {
			return super.updates(sql.getSql(),sql.getParams())>0;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public boolean update(MembershipCard model,BigDecimal recharge){
		Connection con=null;
		try {
			con=super.getConnection(false);
			SqlBuilder sql=SqlBuilder.createUpdate("membershipcard");
			BigDecimal old=new BigDecimal(model.getBalane());
			old.setScale(2);
			sql.addField("idtype",model.getIdtype());
			if(StringUtils.isNotBlank(model.getIdcard())){
				sql.addField("idcard",model.getIdcard());
			}
			if(StringUtils.isNotBlank(model.getAddress())){
				sql.addField("address",model.getAddress());
			}
			sql.addField("sex",model.getSex());
			if(StringUtils.isNotBlank(model.getBirthday())){
				sql.addField("birthday",model.getBirthday());
			}
			if(StringUtils.isNotBlank(model.getProfession())){
				sql.addField("profession",model.getProfession());
			}
			sql.addField("balane",old.add(recharge));
			sql.addField("status",model.getStatus());
			sql.addField("mtid",model.getMtid());
			sql.addWhere("mid",model.getMid());
			int i=super.updates(sql.getSql(),sql.getParams());
			int j=1;
			int k=1;
			if(recharge.intValue()==0){
				j=super.updates("insert into loguser(logid,uid,logtype,logcontent,createtime) values(?,?,?,?,?)",
						UUID.randomUUID().toString().replace("-","").toUpperCase(),model.getUid(),recharge.intValue()>0?7:8,"用户id："+model.getUid()+",在"+DateUtil.currentTime()+"进行了"+
						"变更会员卡余额操作"+recharge+"元",DateUtil.currentTime());
				k=super.updates("insert into logaccount(laid,uid,amount,items,logtype,createtime) values(?,?,?,?,?,?)",UUID.randomUUID().toString().replace("-","").toUpperCase(),
				model.getUid(),recharge,"会员卡余额变更",recharge.intValue()>0?"1":"0",DateUtil.currentTime());
			}
			if(i==1&&j==1&&k==1){
				super.commit(con);
				return true;
			}else{
				super.rollback(con);
				return false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			try {
				super.rollback(con);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
		return false;
	}
	
	public MembershipCard find(String mid){
		try {
			return super.show("select * from membershipcard where 1=1 and mid=?",mid);
		} catch (SQLException e) {
			e.printStackTrace();
			return new MembershipCard();
		}
	}
}
